package com.jjh.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.http.HttpRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.jjh.common.controller.BaseController;
import com.jjh.common.pojo.InfoEnum;
import com.jjh.common.util.ConstatFinalUtil;
import com.jjh.common.util.PageInfoUtil;
import com.jjh.users.pojo.ACourse;
import com.jjh.users.pojo.ASelectCourse;
import com.jjh.users.pojo.AStudent;
import com.jjh.users.pojo.AStudentFav;
import com.jjh.users.pojo.ATeacher;
import com.jjh.users.service.IUsersService;

/**
 * 前台的controller
 * @author MyJJh
 *
 */
@Controller
@RequestMapping("/head/students")
public class StudentsHeadController extends BaseController
{
	/**
	 * service
	 */
	@Autowired
	private IUsersService usersService;
	
	
	/**
	 * 前台登录后主页
	 * @return
	 */
	@RequestMapping("/mainStudent")
	public String mainTeacher()
	{
		ConstatFinalUtil.LOGGER.info("=======mainStudent======");
		return null ;
	}
	
	/**
	 * 前台欢迎界面
	 * @return
	 */
	@RequestMapping("/welcomeStudent")
	public String welcome()
	{
		ConstatFinalUtil.LOGGER.info("=======welcomeStudent======");
		return null ;
	}
	
	
	/**
	 * 课程列表
	 * @return
	 */
	@RequestMapping("/coursesList")
	public String coursesList(HttpServletRequest request , Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====coursesList=======");
		
		/* 获取分页工具 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		
		/* 获取搜索的条件 */
		Map<String,Object> condMap = this.proccedSearch(request);
		
		/* 调用service的方法 */
		List<ACourse> list = this.usersService.findCondListCourseService(pageInfoUtil, condMap);
		
		/* 将条件存储到四大作用中 */
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		model.addAttribute("list", list);
		
		return null ;
	}
	
	
	/**
	/**
	 * 学生收藏课程
	 * @return
	 */
	@RequestMapping(value = "/studentFav",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String studentFav(int courseId , HttpSession session ,Model model)
	{
		ConstatFinalUtil.LOGGER.info("============studentFav=============");
		/* 创建一个学生收藏表 */
		AStudentFav stundentFav = new AStudentFav();
		
		AStudent stundent = (AStudent) session.getAttribute("StudentsSess");
		
		stundentFav.setStudentId(stundent.getId());
		stundentFav.setCourseId(courseId);
		
		JSONObject resultjson = this.usersService.saveOneStudentFavService(stundentFav);
		
		return resultjson.toJSONString() ;
	}
	
	
	/**
	 * 学生选课
	 * @return
	 */
	@RequestMapping(value = "/studentSelect",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String studentSelect(int courseId, HttpSession session ,Model model)
	{
		ConstatFinalUtil.LOGGER.info("==============studentSelect======================");
		/* 创建一个选课表 */
		ASelectCourse selectCourse = new ASelectCourse();
		selectCourse.setClassId(courseId);
		AStudent stundent = (AStudent) session.getAttribute("StudentsSess");
		selectCourse.setStudentsId(stundent.getId());
		
		/**
		 * 根据课程id查询课程
		 */
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("id", courseId);
		
		ACourse course = this.usersService.findOneCourseService(condMap);
		
		/* 设置选课信息 */
		selectCourse.setClasName(course.getName());
		selectCourse.setClasScore(course.getScore());
		
		if(stundent.getBalanceScore() > 20)
		{
			this.info = "总学分已经修够了" ;
			return info ;
		}
		
		/* 学生的分数 */
		stundent.setBalanceScore(stundent.getToalScore() - course.getScore());
		
		/* 更新课程 */
		course.setSignNunm(course.getSignNunm() + 1);
		course.setRemainNum(course.getTotalNum() - course.getSignNunm());
		
		
		/* 更新学生的信息 */
		this.usersService.updateOneStudentService(stundent);
		/* 更新课程 */
		this.usersService.updateOneCourseService(course);
		/* 保存选课信息 */
		JSONObject resultJson = this.usersService.saveOneSelectCourseService(selectCourse);
		
		return resultJson.toJSONString() ;
	}
	
	
	
	
	
	/**
	 * 学生收藏列表
	 * @return
	 */
	@RequestMapping("/studentsFavList")
	public String studentsFavList(HttpServletRequest request , Model model , HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("=====studentsList=======");
		
		/* 获取分页工具 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		
		/* 获取学生的信息 */
		AStudent stundent = (AStudent) session.getAttribute("StudentsSess");
		Map<String,Object> otherMap = new HashMap<String, Object>();
		
		otherMap.put("id", stundent.getId());
		
		
		/* 调用service的方法 */
		List<AStudentFav> list = this.usersService.findCondListStudentFavService(pageInfoUtil, otherMap);
		
		/* 将条件存储到四大作用中 */
		model.addAttribute("pageInfoUtil", pageInfoUtil);
		model.addAttribute("list", list);
		
		return null ;
	}
	
	
	/**
	 * 打开更新页面
	 * @return
	 */
	@RequestMapping("/studentUpdate")
	public String studentUpdate(String operType,  Model model , HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("=====studentUpdate=======");
		AStudent stundent = (AStudent) session.getAttribute("StudentsSess");
		/* 传入一个id */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("id", stundent.getId());
		AStudent students = this.usersService.findOneStudentService(condMap);
		model.addAttribute("students", students);
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 修改个人信息 */
			return "/head/students/updateInfo" ;
		}
		/* 查看用户信息 */
		return "head/students/studentsInfo" ;
	}
	
	/**
	 * 更新提交页面
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/studentUpdateSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String studentUpdateSubmit(String operType , MultipartFile file , Model model , HttpServletRequest request , HttpSession session) throws IOException
	{
		ConstatFinalUtil.LOGGER.info("=====studentUpdateSubmit=======");
		
		AStudent stundent = (AStudent) session.getAttribute("StudentsSess");
		/* 传入一个id */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("id", stundent.getId());
		AStudent students = this.usersService.findOneStudentService(condMap);
		model.addAttribute("students", students);
		model.addAttribute("operType", operType);
		
		Map<String,Object> paramMap = new HashMap<String, Object>();
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 修改个人信息 */
			String nickName = request.getParameter("nickName");
			String qq = request.getParameter("qq");
			String phone = request.getParameter("phone");
			String trueName = request.getParameter("trueName");
			
			/* 存储到数据库中 */
			students.setNickName(nickName);
			students.setQq(qq);
			students.setPhone(phone);
			students.setTrueName(trueName);
			
			/* 上传头像 */
			String fileName = file.getOriginalFilename();
			InputStream is = file.getInputStream();
			
			/* 存储数据 */
			paramMap.put("fileName", fileName);
			paramMap.put("is",is);
			
		}
		
	
		/* 更新用户 */
		JSONObject resultJson = this.usersService.updateOneStudentService(students,paramMap);
		ConstatFinalUtil.LOGGER.info("数据库返回:{}",resultJson.toJSONString());
		/* 提示信息 */
		this.info = resultJson.getString(InfoEnum.VAR_INFO.getInfo());
		return resultJson.toJSONString();
	}
	
	
	
	/**
	 * 选课列表
	 * @return
	 */
	@RequestMapping("/selectCourseList")
	public String selectCourse(HttpServletRequest request ,HttpSession session )
	{
		ConstatFinalUtil.LOGGER.info("=======selectCourse========");
		/* 获取学生的信息 */
		AStudent stundent = (AStudent) session.getAttribute("StudentsSess");
		Map<String,Object> condMap = new HashMap<String, Object>();
		
		condMap.put("id", stundent.getId());
		PageInfoUtil pageInfoUtil = this.proccedPageInfoUtil(request);
		/* 查询对应学生所选的课程 */
		List<ASelectCourse> list = this.usersService.findCondListSelectCourseService(pageInfoUtil, condMap);
		
		/* 存储到四大作用中 */
		request.setAttribute("list", list);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		return null ;
	}
	
	/**
	 * 前台退出
	 * @return
	 */
	@RequestMapping("/loginOutHead")
	public String loginOutHead(HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("==student===loginOut=======");
		session.removeAttribute("StudentsSess");
		session.removeAttribute("lastLoginTime");
		return "redirect:/loginHead" ;
	}
}
